﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace SpriteSheetKit
{
    public class SpritePlayer
    {
        List<Bitmap> Sprites;
        PictureBox pb;
        Timer ticker = new Timer();
        int framecounter = 0;

        public SpritePlayer(List<Bitmap> sprites, PictureBox pbToAnimate, int animationIntervalPerFraminMS)
        {
            Sprites = sprites;
            pb = pbToAnimate;

            ticker.Interval = animationIntervalPerFraminMS;
            ticker.Tick += new EventHandler(ticker_Tick);
        }

        void ticker_Tick(object sender, EventArgs e)
        {
            if (Sprites.Count == 0)
                return;

            framecounter++;

            if (framecounter > Sprites.Count - 1)
               framecounter = 0;

            pb.Image = Sprites[framecounter];
            pb.Invalidate();
        }

        public void Play()
        {
            if (Sprites.Count == 0)
                return;

            framecounter = 0;

            pb.Image = Sprites[0];
            pb.Invalidate();

            ticker.Start();
        }

        public void Stop()
        {
            ticker.Stop();
        }

        public int FrameInterval
        {
            get
            {
                return ticker.Interval;
            }
            set
            {
                ticker.Interval = value;
            }
        }

    }
}
